#include <cstdio>
#include <algorithm>
#include <map>
#include <vector>
#include <string>
#include <iostream>
using namespace std;
map<string,map<string,int>> res;
int main(void){
    int t,n,num;
    string ap,ad;
    scanf("%d",&t);
    while(t--){
        res.clear();
        scanf("%d",&n);
        while(n--){
            cin >> ap >> ad >> num;
            res[ad][ap]+=num;
        }
        map<string,map<string,int>>::iterator it=res.begin();
        for(;it!=res.end();it++){
            cout << it->first << endl;
            map<string,int>::iterator itt=it->second.begin();
            for(;itt!=it->second.end();itt++){
                cout << "   |----" << itt->first << "(" << itt->second << ")" << endl;
            }
        }
        if(t){
            cout << endl;
        }
    }
    return 0;
}